package com.itheima.demo01;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class SendMassageDemo {
    public static void main(String[] args) throws IOException {
        //使用 UDP 发送数据  不面向连接
        //特点：发送速度快，最多只能发送64kb数据，

        //1.创建UDP DatagramSocket 对象
        DatagramSocket socket = new DatagramSocket();

        //2.打包数据
        String str = "你好";
        byte[] bt= str.getBytes();
        InetAddress localAddress = InetAddress.getByName("127.0.0.1");
        int port = 10086;
        DatagramPacket dp = new DatagramPacket(bt,0,bt.length,localAddress,port);

        //3、发送数据
        socket.send(dp);

        //4.释放资源
        socket.close();
    }
}
